using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace TST.SharePoint.Shared.WebControls
{
    /// <summary>
    /// Template object for a column. To be used in a Grid object. 
    /// Render a non-clickable checkbox, based on a boolean value in the datasource.
    /// </summary>
    public class BooleanColumn : ITemplate
    {
        private string _fieldName;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="boolFieldName">The field name of the boolean field.</param>
        public BooleanColumn(string fieldName)
        {
            _fieldName = fieldName;
        }

        public void InstantiateIn(Control container)
        {
            if (container != null)
            {
                Label text = new Label();
                text.ID = string.Concat("BooleanColumn_", _fieldName);
                text.DataBinding += new EventHandler(text_DataBinding);
                container.Controls.Add(text);
            }
        }

        void text_DataBinding(object sender, EventArgs e)
        {
            Label text = (Label)sender;
            if (!string.IsNullOrEmpty(_fieldName))
            {
                GridViewRow container = (GridViewRow)text.NamingContainer;
                if (((System.Data.DataRowView)container.DataItem)[_fieldName] != System.DBNull.Value)
                {
                    string value = ((System.Data.DataRowView)container.DataItem)[_fieldName].ToString();
                    text.Text = (value.ToLower() == "true" ? "Yes" : "No");
                }
            }
        }

    }
}
